import React from 'react';
import { list, anyReq } from '../services/resource';

export default {
  namespace: 'resource',

  state: {
    data: [],
    total: 0,
    result: '', // 返回结果
  },

  effects: {
    *list({ payload }, { call, put }) {
      const response = yield call(list, payload);
      yield put({ type: 'callbackList', response });
    },
    *anyReq({ payload, url }, { call, put }) {
      const response = yield call(anyReq, payload, url);
      yield put({ type: 'callbackAnyReq', response });
    },
  },

  reducers: {
    callbackList(st, { response }) {
      const state = st;
      if (response) {
        state.data = response.root.map((item) => {
          const { id } = item;
          let sequ = id.replace('_', '.');
          sequ = parseFloat(sequ);
          return { ...item, sequ };
        }).sort((a, b) => a.sequ - b.sequ);
        state.total = response.total;
      }
      return { ...state };
    },
    callbackAnyReq(st, { response }) {
      const state = st;
      state.result = response;
      return { ...state }
    },
  }
};
